/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.detector;

import plagiadetector.datatype.TextSegment;
import java.util.ArrayList;
import plagiadetector.datatype.CaseData;

/**
 * Abstract class of a detector.
 * @author Dell
 */
public abstract class Detector {
    
    public String rawText; //unpreprocessed text.
    public String preprocessedText; //preprocessed text. this part is made public so it can be edited if marker exists.
    protected ArrayList<TextSegment> segmentedText; //list of segmentedText
    public ArrayList<CaseData> cases; //list of cases
    
    public Detector(){
        initialize();
    }
    
    /**
     * Initialize this detector to process new file.
     */
    public void initialize(){
        rawText = null;
        preprocessedText = null;
        
        if(segmentedText == null){
            segmentedText = new ArrayList<TextSegment>();
        }else{
            segmentedText.clear();
        }
        
        if(cases == null){
            cases = new ArrayList<CaseData>();
        }else{
            cases.clear();
        }
        
    }
    
    /**
     * Reset parameters of this detector to default.
     */
    public abstract void resetParameters();
    
    /**
     * Set this object's raw text with s.
     * @param s text that will be set.
     */
    public void setRawText(String s){
        rawText = s;
    }
    
    
    /**
     * Preprocess the text of this object.
     */
    public abstract void doPreprocessing();
    
    /**
     * Use this detector preprocessing method to preprocess a string.
     * @param s String that will be preprocessed.
     * @return preprocessed string.
     */
    public abstract String preprocessText(String s);
    
    /**
     * Segment the preprocessed text of this object.
     */
    public abstract void doSegmentation();
    
    /**
     * Extract the stylometric features from the segmented text.
     */
    public abstract void doStylometricExtraction();
    
    /**
     * Quantify and analyze the extracted stylometric features. 
     * This phase fills the detected cases.
     */
    public abstract void doStylometricQA();
    
    /**
     * Get the preprocessed text of this object.
     * @return preprocessed text of this object.
     */
    public String getPreprocessedText(){
        return preprocessedText;
    }
    
    /**
     * Get the raw text of this object.
     * @return raw text of this object.
     */
    public String getRawText(){
        return rawText;
    }
    
    /**
     * Get the number of text segments detected by this object.
     * @return Number of segments.
     */
    public int getNumOfTextSegments(){
        return segmentedText.size();
    }
    /**
     * Get the number of cases detected by this object.
     * @return Number of cases.
     */
    public int getNumOfCases(){
        return getCases().size();
    }
    
    /**
     * Get the text segment of given index.
     * @param i Index of the text segment.
     * @return 
     */
    public TextSegment getTextSegment(int i){
        return segmentedText.get(i);
    }
    
    /**
     * Get a case with given index.
     * @param i case index.
     * @return the case.
     */
    public CaseData getCase(int i){
        return getCases().get(i);
    }
    
    /**
     * Get the text of the case with given index.
     * @param i case index.
     * @return  case text.
     */
    public String getCaseText(int i){
       return getCaseText(getCase(i));
    }
    
    /**
     * Get the text of given CaseData.
     * @param casedata CaseData object.
     * @return  case text.
     */
    public String getCaseText(CaseData casedata){
        return preprocessedText.substring(casedata.offset, casedata.offset+casedata.length);
    }

    /**
     * @return the cases
     */
    public ArrayList<CaseData> getCases() {
        return cases;
    }

    /**
     * Add a new case to the list of cases.
     * @param offset case start position in preprocessed text
     * @param length length of the case (in characters)
     */
    public void addCase(int offset, int length) {
        cases.add(new CaseData(offset,length));
    }

    /**
     * Add new case to the list of cases.
     * @param casedata CaseData object
     */
    public void addCase(CaseData casedata) {
        cases.add(casedata);
    }
    
    
    /**
     * Add new text segment to the list of text segment.
     * @param txt TextSegment string
     * @param pos text start position
     */
    public void addTextSegment(String txt, int startpos) {
        segmentedText.add(new TextSegment(txt, startpos));
    }


    /**
     * Add new text segment to the list of text segment.
     * @param textseg TextSegment object
     */
    public void addTextSegment(TextSegment textseg) {
        segmentedText.add(textseg);
    }
 
 
}
